/**
* @package JCE Media Manager - JCE Player Extension
* @copyright Copyright (C) 2005 - 2010 Ryan Demmer. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see licence.txt
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

WFMediaPlayer.init({
	params : {
		extensions : 'mp3,mp4,flv,f4v',
		dimensions : {
			'audio'	: {
				width 	: 300,
				height 	: 35
			}
		},
		path : 'media/jce/swf/mediaplayer.swf'
	},
	
	props : {
		autoPlay 			: 0,
		bufferingOverlay 	: 0,
		controlBarAutoHide 	: 1,
		controlBarMode		: 'docked',
		loop				: 0,
		muted				: 0,
		playButtonOverlay	: 1,
		bufferingOverlay	: 1,
		volume				: 1,
		audioPan			: 0
	},
	
	type : 'flash',

	isSupported : function(o) {	
		var r, file = '', ext = tinymce.explode(this.getParam('extensions')).join('|'), re = new RegExp('\.(' + ext + ')$');
		
		var src 		= o.src || o.data || '';
		var flashvars 	= o.flashvars || '';

		if (flashvars) {
			var fv = this.parseValues(flashvars);
			if (fv) {
				file = fv.src || ''; 
			}
		}
		
		// check src and flashvars file for format
		r = re.test(src) || re.test(file);
		
		if (!r) {
			// check src for player file
			return new RegExp(this.getPath()).test(src);
		}
		
		return r;
	},
	
	/**
	 * Return player values
	 * @param {String} s FLV file path
	 */
	getValues: function(s) {		
		var self = this, data = [];
		
		// add src
		data.push('src=' + encodeURIComponent($.String.path(tinyMCEPopup.getParam('document_base_url'), s)));
			
		$(':input', '#mediaplayer_options').each(function() {
			var k = $(this).attr('id'), v = $(this).val();
			// remove mediaplayer_ prefix
			k = k.substr(k.indexOf('_') + 1);
			
			if (k == 'volume') {
				if (v > 1) {
					v = v / 100;
				}
			}
			
			if ($(this).is(':checkbox')) {
				v = $(this).is(':checked') ? 1 : 0;
			}

			if (self.props[k] === v || v == '') {
				return;
			}
			data.push(k + '=' + encodeURIComponent(v));
		});

		return data.join('&');
	},
	
	parseValues : function(s) {
		var data = {}, o = $.String.query(s);
		
		$.each(o, function(k, v) {
			switch(k) {
				case 'src' :
					data['src'] = tinyMCEPopup.editor.convertURL(v);
					break;
				case 'volume':
					data['mediaplayer_volume'] = v * 100;
					break;
				default:
					data['mediaplayer_' + k] = v;
					break;
			}
		});
		
		return data;
	},
	
	/**
	 * Set returned player values
	 * @param {String} fv Flashvars value
	 * @param {Object} p Parameter Object
	 */
	setValues: function(fv, p) {
		var at = this.parseValues(fv);
		
		$.each(at, function(k, v) {
			$('#' + k).val(v);
		});
	},
	
	/**
	 * Action when file selected
	 */
	onSelectFile: function() {},
	
	/**
	 * Action when file is inserted.
	 * Use to set variables for MediaPlayer format
	 */
	onInsert : function() {
		var src = $('#src').val(), mp3 = /\.mp3$/.test(src), dimensions = this.getParam('dimensions');
		
		if (mp3 && dimensions.audio) {
			$('#width').val(dimensions.audio.width);
			$('#height').val(dimensions.audio.height);
		}
		
		// set some flash variables
		$('#flash_wmode').val('opaque');
		$('#flash_allowfullscreen').attr('checked', !mp3);
		$('#flash_menu').attr('checked', true);
	}
});
